<?php
/**
 *
 * @category   babel 
 * @package    BABEL
 * @author     Hannes Magnusson <hannes.magnusson@gmail.com>
 * @copyright  2008 Team babel
 * @license    LGPL
 */

class BabelDBLanguageFile extends ArrayObject {
	const NOT_FOUND    = 404;
	const FOUND        = 302;
	const NOT_MODIFIED = 304;
	private static $langids;

	public function __construct($filename, array $meta = null) {
		$meta = (array) $meta; 
		$meta["filename"] = $filename;
		parent::__construct($meta);
	}
	public function setDBData($data) {
		foreach($data as $k => $v) {
			$this[$k] = $v;
		}
	}
	public function updateRevision($time) {
		return PDODB::runQuery("UPDATE babel_files SET revision = FROM_UNIXTIME(?) AND changed_date = NOW() WHERE file_id=?", array($time, $this["id"]));
	}
	public function insertFilename($time) {
		$retval = PDODB::execQuery("INSERT INTO babel_files (name, created, changed_date) VALUES(?, NOW(), FROM_UNIXTIME(?))", array($this["filename"], $time), $id);
		$this["id"] = $id;
		return $retval;
	}
	public function getRevision() {
		return $this["revision"];
	}
	public function getConstants() {
		$success = PDODB::runQuery("
SELECT
	babel_originals.original_id AS id,
	babel_originals.constant_name AS name,
	babel_values.original_string AS value
FROM
	babel_values, babel_originals
WHERE
	babel_originals.file_id=? 
AND
	babel_values.original_id=babel_originals.original_id
		", array($this["id"]), $stmt);
		if (!$success) {
			throw new Exception("There was an error with the DB connection and/or query");
		}

		$a = array();
		foreach($stmt as $row) {
			$a[$row["name"]] = array(
				"value" => $row["value"],
				"id" => $row["id"],
			);
		}
		$this["constants"] = $a;
	}
	public function hasConstant($constant) {
		if (isset($this["constants"][(string)$constant])) {
			if ($this["constants"][(string)$constant]["value"] == $constant->getValue()) {
				return self::NOT_MODIFIED;
			}
			return self::FOUND;
		}
		return self::NOT_FOUND;
	}
	public function insertConstant($constant) {
		$success = PDODB::execQuery("INSERT INTO babel_originals (file_id, constant_name, created, context) VALUES(?, ?, NOW(), ?)", array($this["id"], (string)$constant, $constant->getContext()), $id);
		if (!$success) {
			throw new Exception("Failed");
		}
		$this->insertLangConstant($id);
		return $this->updateConstant($id, $constant->getValue());
	}
	protected function insertLangConstant($id) {
		static $stmt;

		if (!isset(self::$langids)) {
			$ids = PDODB::execQueryFetch("SELECT language_id AS id FROM babel_languages", PDO::FETCH_COLUMN);
			self::$langids = $ids;
		}
		if (!isset($stmt)) {
			$pdo = PDODB::connect();
			$stmt = $pdo->prepare("INSERT INTO
		babel_translations (revision, translated_string, status, created,               language_id, value_id, changed_date)
					VALUES (0,        '',                ?,      '1970-01-01 00:00:00', ?,           ?,        '1970-01-01 00:00:00')");
		}

		foreach(self::$langids as $langid) {
			$stmt->execute(array(BABEL_TRANSLATION_TRANSLATION, $langid, $id));
		}
	}
	public static function getAllLangs() {
		return self::$langids;
	}

	public function updateConstant($id, $value) {
		return PDODB::runQuery("INSERT INTO babel_values (original_string, created, original_id) VALUES(?, NOW(), ?)", array($value, $id));
	}
}

